using System;
using System.Collections;
using System.Data.Linq.Mapping;
using System.Runtime.Serialization;
using Portal.Data.Helper;
using Portal.Core.Binding.FormBuilder;
using Portal.Core.Helper;

namespace Portal.Data.Entity
{
    [Table(Name = "dbo.WebpartDefinition"), Serializable, DataContractAttribute]
    public class WebpartDefinition : BaseObject
    {
        public WebpartDefinition()
        {
            Editor = "Webparts/GenericEditor.ascx";
        }
        #region Properties
        public int ID { get { return WebPartDef_ID; } set { WebPartDef_ID = value; } }

        [SkipBinding]
        [ColumnAttribute(Name = "WebPartDef_ID", IsPrimaryKey = true, IsDbGenerated = true)]
        public int WebPartDef_ID { get; set; }

        [ColumnAttribute(Name = "Name"), Validator(Required = true), Unique]
        public string Name { get; set; }

        [Column]
        public string Editor { get; set; }

        [ColumnAttribute(Name = "Path"), Validator(Required = true)]
        public string Path { get; set; }

        [ColumnAttribute(Name = "Icon"), Builder(DataType = DataType.Image), Image(SavePath = "Images/Webpart/")]
        public string Icon { get; set; }
        #endregion

        #region Extension Props
        public string RenderImage
        {
            get
            {
                return ImageHelper.RenderImageFlash(Icon, 100, 100);
            }
        }
        #endregion

        #region Methods
        public static WebpartDefinition FindByPrimaryKey(object objectid)
        {
            return FindByPrimaryKey<WebpartDefinition>(objectid);
        }
        public static WebpartDefinition FindByPrimaryKeyRecache(object objectid)
        {
            return FindByPrimaryKey<WebpartDefinition>(objectid, true);
        }
        public static IList Paging(int pagesize, int pageindex, out int count)
        {
            var parameter = new object[] { typeof(WebpartDefinition).Name, "Paging", pageindex };
            var obj = CacheHelper.Instance.Get(parameter);
            if (obj != null)
            {
                var values = (object[])obj;
                count = (int)values[1];
                return (IList)values[0];
            }

            //end get cache -------------------
            const string hql = "select * from WebpartDefinition";
            const string order = "order by Name";
            var list = LINQHelper.SlicedFindAll<WebpartDefinition>(hql, order, pageindex * pagesize, pagesize, out count);
            var newvalues = new object[] { list, count };
            CacheHelper.Instance.Set(parameter, newvalues);

            return list;
        }
        public static IList ListAll()
        {
            var parameter = new object[] { typeof(WebpartDefinition).Name, "ListAll" };
            var obj = CacheHelper.Instance.Get(parameter);
            if (obj != null)
            {
                return (IList)obj;
            }
            var result = LINQHelper.GetList<WebpartDefinition>("select * from WebpartDefinition order by Name");
            CacheHelper.Instance.Set(parameter, result);
            return result;
        }
        public override void Delete()
        {
            base.Delete();
            //remove references
            LINQHelper.ExecuteNonQuery("DELETE from [Webpart] where Definition_ID={0}", ID);
            Recache();
        }
        public override void AfterSaveUpdate()
        {
            base.AfterSaveUpdate();
            Recache();
        }

        public void Recache()
        {
            if (ID > 0)
                FindByPrimaryKeyRecache(ID);
            var parameter = new object[] { typeof(WebpartDefinition).Name, "ListAll" };
            CacheHelper.Instance.Set(parameter, null);
            parameter = new object[] { typeof(WebpartDefinition).Name, "Paging", 0 };
            CacheHelper.Instance.Set(parameter, null);
        }
        #endregion Methods
    }
}
